package com.pro.db.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.pro.db.domain.GuopuCategory;
import com.pro.db.dao.GuopuCategoryMapper;
import com.pro.db.service.itf.IGuopuCategoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pro.db.util.ResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 * 栏目表 服务实现类
 * </p>
 *
 * @author wanglei
 * @since 2019-12-21
 */
@Service
public class GuopuCategoryServiceImpl extends ServiceImpl<GuopuCategoryMapper, GuopuCategory> implements IGuopuCategoryService {

    @Autowired
    GuopuCategoryMapper categoryMapper;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Object sortUp(GuopuCategory category) {
        Integer id = category.getId();
        if (id == null) {
            return ResponseUtil.badArgument("栏目ID ");
        }
        GuopuCategory nowGoods = categoryMapper.selectById(id);
        if(null == nowGoods){
            return ResponseUtil.badArgument("栏目不存在 ");
        }
        GuopuCategory largeOne = categoryMapper.selectOne(new QueryWrapper<GuopuCategory>()
            .lt("push_sort",nowGoods.getPushSort())
            .eq("level",nowGoods.getLevel())
            .orderByDesc("push_sort"));
        if(null == largeOne){
            return ResponseUtil.fail("已是首位，无法移动");
        }else {
            category.setPushSort(largeOne.getPushSort());
            largeOne.setPushSort(nowGoods.getPushSort());
        }
        categoryMapper.updateById(category);

        GuopuCategory updateGoods = new GuopuCategory();
        updateGoods.setId(largeOne.getId());
        updateGoods.setPushSort(largeOne.getPushSort());
        categoryMapper.updateById(updateGoods);
        return ResponseUtil.ok();
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Object sortDown(GuopuCategory category) {
        Integer id = category.getId();
        if (id == null) {
            return ResponseUtil.badArgument("栏目ID");
        }
        GuopuCategory nowGoods = categoryMapper.selectById(id);
        if(null == nowGoods){
            return ResponseUtil.badArgument("栏目不存在");
        }
        GuopuCategory largeOne = categoryMapper.selectOne(new QueryWrapper<GuopuCategory>()
                .gt("push_sort",nowGoods.getPushSort())
                .eq("level",nowGoods.getLevel())
                .orderByAsc("push_sort"));
        if(null == largeOne){
            return ResponseUtil.fail("已是最末，无法移动");
        }else {
            category.setPushSort(largeOne.getPushSort());
            largeOne.setPushSort(nowGoods.getPushSort());
        }
        categoryMapper.updateById(category);

        GuopuCategory updateGoods = new GuopuCategory();
        updateGoods.setId(largeOne.getId());
        updateGoods.setPushSort(largeOne.getPushSort());
        categoryMapper.updateById(updateGoods);
        return ResponseUtil.ok();
    }
}
